﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormProject.Diger
{
    public partial class Kategorilerfrm : Form
    {
        public Kategorilerfrm()
        {
            InitializeComponent();
        }
        DBBaglantiDataContext db = new DBBaglantiDataContext();
        public BindingSource  MySourceNew =null;//new BindingSource();
        public DataSet MyDataSet = new DataSet();
    
        private void Kategorilerfrm_Load(object sender, EventArgs e)
        {
            ////MyDataSet.Tables.Add(KategoriGetir());           
            ////MySourceNew.DataSource = MyDataSet;
            ////MySourceNew.DataMember = MyDataSet.Tables["Kategoriler"].TableName;
            ////GrdKategoriler.DataSource = MySourceNew;
            ////MySourceNew.Position = MySourceNew.Count;
            ////bindingNavigator1.BindingSource = MySourceNew;
            ////MySourceNew.PositionChanged += new EventHandler(MySourceNew_PositionChanged);         



            KategoriDoldur();
        }

        void MySourceNew_PositionChanged(object sender, EventArgs e)
        {
            try
            {
                    KatagoriAdi.DataBindings.Clear();
                    KatagoriAdi.DataBindings.Add("Text", MySourceNew, "KatagoriAdi"); 
            }
            catch (Exception)
            {               

            }
        }

        public  DataTable KategoriGetir()
        {   
            var sorgu = (from x in db.Kategorilers select x);            
            SqlCommand cmd = db.GetCommand(sorgu) as SqlCommand;
            DataTable dt = new DataTable("Kategoriler");
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            return dt; 
        }

        private void KategoriDoldur()
        {
            //table = KategoriGetir();
            ////MySourceNew = new BindingSource();
            //MySourceNew.DataSource = MyDataSet;
            //MySourceNew.DataMember = MyDataSet.Tables["Kategoriler"].TableName;
            //GrdKategoriler.DataSource = MySourceNew;
            //MySourceNew.Position = MySourceNew.Count;
            //bindingNavigator1.BindingSource = MySourceNew;
            //MySourceNew.PositionChanged += new EventHandler(MySourceNew_PositionChanged);
            //GrdKategoriler.DataSource = table;
            //////////GrdKategoriler.DataSource = KategoriGetir();

            if (MyDataSet.Tables["Kategoriler"]!=null)
            {
                MyDataSet.Tables.Remove("Kategoriler");  
            }
            
            MyDataSet.Tables.Add(KategoriGetir());
            MySourceNew = new BindingSource();
            MySourceNew.DataSource = MyDataSet;
            MySourceNew.DataMember = MyDataSet.Tables["Kategoriler"].TableName;            
            GrdKategoriler.DataSource = MySourceNew;
            MySourceNew.Position = MySourceNew.Count;
            bindingNavigator1.BindingSource = MySourceNew;
            MySourceNew.PositionChanged += new EventHandler(MySourceNew_PositionChanged);    


        }


        private void bt_kaydet_Click(object sender, EventArgs e)
        {

            Kategoriler kt = new Kategoriler();
            var KayitKontrol = (from x in db.Kategorilers where x.KatagoriAdi == KatagoriAdi.Text  select x).FirstOrDefault();

            if (KayitKontrol != null)
            {

                KayitKontrol.KatagoriAdi = KatagoriAdi.Text;
                db.SubmitChanges();
                MessageBox.Show("Kayıt Güncellendi");
                return;
            }

            else
            {
                     
                kt.KatagoriAdi = KatagoriAdi.Text;
                db.Kategorilers.InsertOnSubmit(kt);
                db.SubmitChanges();
                KategoriDoldur();
                KatagoriAdi.Text = null;
                KatagoriAdi.Focus();           
               
               
              
              


            }
        }

        private void btn_yeni_Click(object sender, EventArgs e)
        {

            if (MySourceNew.Filter != string.Empty)
            {
                MySourceNew.RemoveFilter();
            }
            MySourceNew.AddNew();
            KatagoriAdi.Text = null;
            KatagoriAdi.Focus();
        }

        private void KatagoriAdi_ara_TextChanged(object sender, EventArgs e)
        {
            if (KatagoriAdi_ara.Text.Trim().Length > 0)
            {
                string Filtre = "KatagoriAdi Like '%" + KatagoriAdi_ara.Text + "%'";
                MySourceNew.Filter = Filtre;
                MySourceNew.Position = MySourceNew.Count - 1;
               
            }
            else
            {
                MySourceNew.RemoveFilter();
                MySourceNew.Position = MySourceNew.Count - 1;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < MyDataSet.Tables.Count ; i++)
            {
                MessageBox.Show(MyDataSet.Tables[i].TableName );
            }
        }

        private void GrdKategoriler_CurrentCellChanged(object sender, EventArgs e)
        {
           
        }

       

    }
}
